package com.tailyou.hms.firstapp.act;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.util.CommonUtils;

/**
 * 菜单具体使用可参考
 * https://blog.csdn.net/m0_58941767/article/details/125056294
 */
public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        findViewById(R.id.btnContextMenu).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startActionMode(new ActionMode.Callback() {
                    @Override
                    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                        getMenuInflater().inflate(R.menu.context_menu, menu);
                        return true;
                    }

                    @Override
                    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                        return false;
                    }

                    @Override
                    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.delete:
                                CommonUtils.showToast(MenuActivity.this, "delete");
                                break;
                            case R.id.back:
                                CommonUtils.showToast(MenuActivity.this, "back");
                                break;
                            case R.id.rename:
                                CommonUtils.showToast(MenuActivity.this, "rename");
                                break;
                            case R.id.exit:
                                CommonUtils.showToast(MenuActivity.this, "exit");
                                break;
                        }
                        return true;
                    }

                    @Override
                    public void onDestroyActionMode(ActionMode mode) {

                    }
                });
                return false;
            }
        });
        Button btnPopupMenu = findViewById(R.id.btnPopupMenu);
        btnPopupMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu menu = new PopupMenu(MenuActivity.this, btnPopupMenu);
                //加载菜单资源
                menu.getMenuInflater().inflate(R.menu.context_menu, menu.getMenu());
                //设置点击事件的响应
                menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.delete:
                                CommonUtils.showToast(MenuActivity.this, "delete");
                                break;
                            case R.id.back:
                                CommonUtils.showToast(MenuActivity.this, "back");
                                break;
                            case R.id.rename:
                                CommonUtils.showToast(MenuActivity.this, "rename");
                                break;
                            case R.id.exit:
                                CommonUtils.showToast(MenuActivity.this, "exit");
                                break;
                        }
                        return true;
                    }
                });
                menu.show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return true;
    }

    //菜单的点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.upload:
                Toast.makeText(this, "保存", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
                break;
            case R.id.back:
                Toast.makeText(this, "退出", Toast.LENGTH_SHORT).show();
                break;
            case R.id.add:
                Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
                break;
            default:
                super.onOptionsItemSelected(item);
        }
        return true;
    }

}